﻿using Abp.Domain.Repositories;
using Abp.Runtime.Session;
using Abp.Timing.Timezone;
using Camc.Quality.Bom.Dto;
using Camc.Quality.DataExporting.Excel.EpPlus;
using Camc.Quality.Dto;
using Camc.Quality.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.Bom.BomInterface
{
    public class BomExportToExcel : EpPlusExcelExporterBase, IBomExportToExcel
    {
        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;
        private readonly IRepository<BomDesign, Guid> _bomDesignrepository;
        private readonly IRepository<BomDesignDetail, Guid> _bomDesignDetailrepository;
        public BomExportToExcel(
            ITimeZoneConverter timeZoneConverter,
            IAbpSession abpSession,
            ITempFileCacheManager tempFileCacheManager, IRepository<BomDesign, Guid> bomDesignrepository, IRepository<BomDesignDetail, Guid> bomDesignDetailrepository)
            : base(tempFileCacheManager)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
            _bomDesignDetailrepository = bomDesignDetailrepository;
            _bomDesignrepository = bomDesignrepository;
        }
        public string ExportToFile(List<BomEditChildData>  bomDetaillists)
        {
            return CreateExcelPackageLocal(
          "BomExcel.xlsx",
          excelPackage =>
          {
              var sheet = excelPackage.Workbook.Worksheets.Add(L("MaterialsContent"));
              sheet.OutLineApplyStyle = true;
              var levelMax = _bomDesignDetailrepository.GetAll().Max(t => t.LevelNum);
              List<string> headerData = new List<string>();
              for (int i = 0; i <= levelMax-1; i++)
              {
                  headerData.Add("层级" + (i + 1));
                  if (i == levelMax-1)
                  {
                      headerData.Add(L("父级物料代码"));
                      headerData.Add(L("物料代码"));
                      headerData.Add(L("物料名称"));
                      headerData.Add(L("用量"));
                      headerData.Add(L("规格型号"));
                      headerData.Add(L("物料属性"));
                      headerData.Add(L("物料类型"));
                      headerData.Add(L("单位"));
                      break;
                  }
              }
          
                     
              AddHeaderDynamic(
                  sheet,
                 headerData
                  );

              AddDynamicObjects(
                  sheet, 2, levelMax, bomDetaillists,
                  _ => _.LevelNum,
                  _ => _.MaterialNo,
                  _ => _.MaterialNo,
                  _ => _.MaterialName,
                  _ => _.Unit1BomDosage,
                  _ => _.Model,
                  _ => _.MaterialPropertyName,
                  _ => _.MaterialTypeName,
                  _ => _.UnitName
                  );
              //var creationTimeColumn = sheet.Column(levelMax + 8);
              //creationTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd HH:mm:ss";

              for (var i = 1; i <= levelMax + 8; i++)
              {
                  sheet.Column(i).AutoFit();
              }
          });
        }
    }
}
